package com.win.oauth.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fyl.core.advance.exception.BaseException;
import com.fyl.schedule.QuartzManager;
import com.win.oauth.entity.Task;
import com.win.oauth.mapper.TaskMapper;
import com.win.oauth.service.ITaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.quartz.Job;
import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fyl
 * @since 2020-09-05
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements ITaskService {

    @Autowired
    QuartzManager quartzManager;

    @Override
    public boolean save(Task entity) {
        this.memoryTaskOpt(entity);
        return super.save(entity);
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        List<Task> tasks = this.listByIds(idList);
        tasks.forEach(this::memoryTaskDelete);
        return super.removeByIds(idList);
    }

    @Override
    public boolean updateById(Task entity) {
        this.memoryTaskOpt(entity);
        return super.updateById(entity);
    }

    @Override
    public Task getById(Serializable id) {
        Task task = super.getById(id);
        Trigger.TriggerState jobState = quartzManager.getJobState(task.getJobName(), task.getJobGroup());
        task.setMemoryStatus(jobState);
        return task;
    }

    @Override
    public <E extends IPage<Task>> E page(E page, Wrapper<Task> queryWrapper) {
        E page1 = super.page(page, queryWrapper);
        page1.getRecords().stream().forEach(e -> e.setMemoryStatus(quartzManager.getJobState(e.getJobName(), e.getJobGroup())));
        return page1;
    }

    private void memoryTaskOpt(Task entity) {
        String jobName = entity.getJobName();
        String jobGroup = entity.getJobGroup();
        String corn = entity.getCron();
        boolean exists = quartzManager.checkExists(jobName, jobGroup);
        if (exists) {
            if (Objects.equals(entity.getDbStatus(), JOB_RUN_STATUS)) {
                quartzManager.updateJob(jobName, jobGroup, corn);
            } else {
                quartzManager.pauseJob(jobName, jobGroup);
            }
        } else {
            if (Objects.equals(entity.getDbStatus(), JOB_RUN_STATUS)) {
                //noinspection unchecked
                quartzManager.addJob(buildJob(entity));

            }
        }
    }

    private void memoryTaskDelete(Task entity) {
        String jobName = entity.getJobName();
        String jobGroup = entity.getJobGroup();
        boolean exists = quartzManager.checkExists(jobName, jobGroup);
        if (exists) {
            quartzManager.deleteJob(jobName, jobGroup);
        }
    }


}
